import json
from llm.doubao import call_doubao_api


def word_info_generator(words):
    with open('llm/word_gen_prompt.txt', 'r', encoding='utf-8') as f:
        prompt_template = f.read()

    for word in words:
        prompt = prompt_template.replace('$word', word)
        response = call_doubao_api(prompt)

        print(response)

        if response.strip() == "INVALID_WORD":
            yield None
        else:
            try:
                word_info = json.loads(response)
                yield {
                    'english': word_info.get('english', word),
                    'chinese': word_info.get('chinese', ''),
                    'phonetic': word_info.get('phonetic', ''),
                    'example': word_info.get('Example', '')
                }
            except json.JSONDecodeError:
                yield None


# Example usage
if __name__ == '__main__':
    words_to_check = ['knowledge', 'apple', 'xyzabc']
    for result in word_info_generator(words_to_check):
        if result:
            print(f"Word: {result['english']}")
            print(f"Chinese: {result['chinese']}")
            print(f"Phonetic: {result['phonetic']}")
            print(f"Example: {result['example']}")
        else:
            print("Invalid word or error in processing")
        print()
